<?php

class IndexController extends Zend_Controller_Action
{

	public function indexAction()
	{

		/**$mail = new Zend_Mail();
		$mail->setBodyText('This is an example message body');
		$mail->setFrom('servermail@localhost', ' BIS Server');
		$mail->addTo('felix.kauschmann@activision.com');
		$mail->setSubject('This is an example subject');
		$mail->send();*/
		
		$strSql = 'SELECT
			sales.netsales,
			SUM(fcst.measure) AS forecast,
			cal.calQuarter
	
		FROM bis.tbl_forecast fcst

		LEFT JOIN (SELECT calPeriod, calQuarter FROM bis.tbl_calendar GROUP BY calPeriod) cal ON cal.calPeriod = fcst.Period
		LEFT JOIN (SELECT SUM(quantity) as netsales, quarter FROM bis.tbl_invoicedSales LEFT JOIN bis.tbl_globalAccount ON bis.tbl_globalAccount.globalAccount= bis.tbl_invoicedSales.globalAccount WHERE year=2013 AND accountSummaryDesc=\'NET REVENUE\' AND bis.tbl_invoicedSales.globalAccount<>\'42500\' GROUP BY quarter) sales ON sales.quarter = cal.calQuarter
		
		WHERE fcst.reportingType=\'Total Units\' AND fcst.yearNbr=\'FY13\' AND fcst.scenario = \'CurrFcst\'
		
		GROUP BY calQuarter';
		
		$dbconn = Zend_Db_Table::getDefaultAdapter();
		
		$this->view->entries = $dbconn->fetchAll($strSql);
		
		
	}
	
	public function printcustomerAction()
	{
		$param = $this->getRequest()->getParam('customernumber');

		$dbconn = Zend_Db_Table::getDefaultAdapter();
		
		$result = $dbconn->fetchPairs('SELECT 
					SUM(i.quantity) AS QTY,
					SUM(i.amount) AS AMT 

					FROM bis.tbl_invoicedSales i 

					WHERE 	i.year=year(now()) 
						AND i.quarter=quarter(now()) 
						AND i.globalAccount=\'40000\'
						AND i.customerNumber =\''. $param .'\'');
		

		$convertedArray = array();
		foreach($result as $key => $value) {
			
			$key = number_format($key, 0, ',', '.');
			$value = number_format($value, 2, ',', '.');
		
			$convertedArray[$key] = $value;
		}	
		
		$this->_helper->json($convertedArray);
	}

}

